﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectFitus.Models;

namespace ProjectFitus.Controllers
{
	[HandleError]
	public class SubSitesController : Controller
    {
        IDBFitusRepository m_repo;

        public SubSitesController()
        { 
            m_repo = new DBFitusRepository();
			System.Web.HttpRuntime.Cache["MenuItemsSub"] = m_repo.GetMenuItemsByParentId(4); //uppfæri submenu fyrir greinar
        }

		protected override void OnException(ExceptionContext fc)
		{
			base.OnException(fc);
			FitusLogger.Instance.LogException(fc.Exception);

		}

        //
        // GET: /ControllerSubSites/

       /* public ActionResult Index()
        {
            return View();
        }
        */
        //
        // GET: /ControllerSubSites/Details/5

        public ActionResult Details(long id)
        {
            var ss = m_repo.GetSubsiteById(id);
            return View(ss);
        }


        //
        // POST: /ControllerSubSites/Create
        /*
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
         

        public ActionResult Index(long id)
        {
            var allSubSite = m_repo.GetSubsiteById(id);
            return View(allSubSite);
        }*/

        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(long? id)
        {
            var subSite = m_repo.GetSubsiteById(id.Value);
			subSite.contentText = Server.HtmlDecode(subSite.contentText);
			
            return View(subSite);
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(SubSite allEdited)
        {
            if (ModelState.IsValid)
            {
				allEdited.lastDateEdited = DateTime.Now;
                allEdited.contentText.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&amp;", "&");
                m_repo.UpdateSubSite(allEdited);
                return RedirectToAction("Details/" + allEdited.id, "SubSites");
            }
            return View("Index", "Home");
        }

        //
        // GET: /ControllerSubSites/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /ControllerSubSites/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
